﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Draglets.UI
{
    [DefaultProperty("Name")]
    [ToolboxData("<{0}:Manager runat=server></{0}:Manager>")]
    public class Manager : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Name
        {
            get
            {
                String s = (String)ViewState["Name"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Name"] = value;
            }
        }

        public bool OverrideJQuery { get; set; }

        /// <summary>
        /// OnLoad
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            this.EnableViewState = false;
            Page.Response.Write("<!--Draglets Manager: " + Name + "-->");

            //Later on... ScriptManager?

            StringBuilder scriptString = new StringBuilder();
            scriptString.Append("\n\n");
            scriptString.Append("<!--Scripts added by Draglets-->\n");
            scriptString.Append("<script type=\"text/javascript\">\n");
            scriptString.Append("    $(document).ready(function() {\n");
            scriptString.Append("       $(\".dragletsWidgetHeader button.config\").button({\n");
            scriptString.Append("           icons: {\n");
            scriptString.Append("               primary: 'ui-icon-gear',\n");
            scriptString.Append("               secondary: 'ui-icon-triangle-1-s'\n");
            scriptString.Append("           },\n");
            scriptString.Append("           text: false\n");
            scriptString.Append("           });\n");
            scriptString.Append("       $(\".dragletsWidgetHeader button.toggle\").button({\n");
            scriptString.Append("           icons: {\n");
            scriptString.Append("               primary: 'ui-icon-carat-2-n-s'\n");
            scriptString.Append("           },\n");
            scriptString.Append("           text: false\n");
            scriptString.Append("           });\n");
            scriptString.Append("   });\n");
            scriptString.Append("</script>\n");
            scriptString.Append("<!--/Scripts added by Draglets-->\n\n");
            Page.Form.Controls.Add(new LiteralControl(scriptString.ToString()));

            base.OnLoad(e);
        }

        protected override void OnInit(EventArgs e)
        {


            StringBuilder scriptString = new StringBuilder();

            scriptString.Append("\n\n");
            scriptString.Append("<!--Scripts added by Draglets-->\n");
            scriptString.Append("<link href=\"http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/start/jquery-ui.css\" type=\"text/css\" rel=\"Stylesheet\" />\n");
            scriptString.Append("<script src=\"http://www.google.com/jsapi\"></script>\n");
            scriptString.Append("<script type=\"text/javascript\">\n");
            scriptString.Append("    google.load(\"jquery\", \"1.4.2\");\n");
            scriptString.Append("    google.load(\"jqueryui\", \"1.8\");\n");
            scriptString.Append("</script>\n");
            scriptString.Append("<style>\n");
            scriptString.Append("    .dragletsWidgetHeader { text-align: right;  }\n");
            scriptString.Append("</style>\n");
            scriptString.Append("<!--/Scripts added by Draglets-->\n\n");

            LiteralControl addScript = new LiteralControl(scriptString.ToString());
            Page.Header.Controls.Add(addScript);

            
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

    }
}
